package com.dht.imagepicker.albums

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.dht.imagepicker.GridItemDecor
import com.dht.imagepicker.R
import com.dht.imagepicker.databinding.ActivityAlbumsBinding
import com.gyf.immersionbar.ImmersionBar

class AlbumsActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAlbumsBinding

    private val adapter = AlbumsAdapter()

    private val viewModel by lazy { ViewModelProvider(this)[AlbumsViewModel::class.java] }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ImmersionBar.with(this).statusBarColor(R.color.color_333333).statusBarDarkFont(false).init()

        binding = ActivityAlbumsBinding.inflate(layoutInflater)

        setContentView(binding.root)

        binding.recyclerView.addItemDecoration(GridItemDecor())
        binding.recyclerView.layoutManager = GridLayoutManager(this, 4)
        binding.recyclerView.adapter = adapter

        initObserve()

        initListener()

        viewModel.fetchImages()
    }

    private fun initListener() {
        binding.backView.setOnClickListener { finish() }
    }

    /**
     * 初始化观察
     */
    private fun initObserve() {
        viewModel.imageInfoMutableLiveData.observe(this) {
            adapter.submitList(it)
        }
    }
}